'use strict';

App.provider('router',['$stateProvider', function ($stateProvider) {

    this.$get = function ($state) {
        return {
            setUpRoutes: function (menus) {
                var router = this;
                for (var i = 0; i < menus.length; i++) {
                    router.setUpRoute(menus[i]);
                }
            },
            setUpRoute: function(menu) {
                var state = 'auth.' + menu.menuUrl;
                if (menu.menuUrl != undefined && menu.menuUrl.length > 0) {
                    if (!$state.get(state)) {
                        $stateProvider.state(state, {
                            url: "/" + menu.menuUrl,
                            templateUrl: 'views/' + menu.templateUrl,
                            data: {pageTitle: menu.menuName, menuId: menu.menuId},
                            controller: menu.ctrlName,
                            resolve: menu.ctrlPath && menu.ctrlPath.length > 0 ? {
                                deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                                    return $ocLazyLoad.load({
                                        name: 'App',
                                        insertBefore: '#ng_load_plugins_before', // load the above css files before '#ng_load_plugins_before'
                                        files: [
                                            'js/controllers/' + menu.ctrlPath
                                        ]
                                    });
                                }]
                            } : {}
                        });
                    }
                }
                if (menu.children) {
                    for (var i = 0; i < menu.children.length; i++) {
                        this.setUpRoute(menu.children[i]);
                    }
                }
            }
        }
    };
}]);

/* Setup Rounting For All Pages */
App.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    // Redirect any unmatched url
    $urlRouterProvider.otherwise(function($injector, $location) {
        if ($injector.get('$rootScope').user) {
            $location.path('/dashboard');
        } else {
            $location.path('/login');
        }
    });

    $stateProvider

        .state('login', {
            url: "/login",
            templateUrl: "layout/login.html",
            controller: "LoginController"
        })

        // auth
        .state('auth', {
            url: "",
            abstract: true,
            templateUrl: "layout/main.html"
        })
        .state('auth.dashboard', {
            url: "/dashboard",
            templateUrl: "views/dashboard.html",
            data: {pageTitle: '首页'},
            controller: "dashboardController",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'js/controllers/dashboardController.js',
                            'assets/global/plugins/jquery-easypiechart/jquery.easypiechart.min.js',
                            'assets/global/plugins/jquery.sparkline.min.js'
                        ]
                    });
                }]
            }

        })

        // 代码生成
        .state('auth.generator', {
            url: "/generator",
            templateUrl: "views/generator/model.html",
            controller: "generatorController"
        })

        .state('auth.test', {
            url: "/test",
            templateUrl: "views/test/test.html",
            controller: "testController",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'js/controllers/test/testController.js'
                        ]
                    });
                }]
            }
        })

        // 示例
        .state('auth.sample', {
            url: "/sample",
            templateUrl: "views/sample/sample.html",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'assets/global/plugins/google-code-prettify/prettify.css',
                            'assets/global/plugins/google-code-prettify/prettify.js'
                        ]
                    });
                }]
            }
        })
        // combo
        //.state('auth.sample.combo', {
        //    url: "/combo",
        //    templateUrl: "views/sample/combo.html"
        //})
}]);
